//
//  StrategyDetailCell.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/25.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "StrategyDetailCell.h"
#import "PhotoCollectionViewCell.h"

@interface StrategyDetailCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    CGFloat x;
    CGFloat cellWidth;
    CGFloat nameHeight;
    //CGFloat collectionHeight;
    //CGFloat titleHeight;
}
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *contents;
//@property (nonatomic, strong) UIView *icon;
@property (nonatomic, strong) NSMutableArray *photoArray;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UILabel *name;

@property (nonatomic, strong) UILabel *time;

@property (nonatomic, strong) UILabel *line;

@end

static NSString *collectionIdentifier = @"cell";
@implementation StrategyDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //collectionHeight = 0;
        self.selectionStyle = NO;
        x = 20 * KScreenWidth / 414;
        cellWidth = KScreenWidth - 2 * x;
        //titleHeight = 0;
        self.title = [[UILabel alloc]initWithFrame:CGRectMake(x, 0, cellWidth, 0)];
        self.title.textAlignment = NSTextAlignmentLeft;
        self.title.font = [UIFont systemFontOfSize:20 * KScreenWidth / 414 weight:2];
        self.title.textColor = [UIColor blackColor];
//        self.title.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.title];
        
        self.contents = [[UILabel alloc]initWithFrame:CGRectMake(x, self.title.height, cellWidth, 10)];
        self.contents.textColor = [UIColor grayColor];
        self.contents.numberOfLines = 0;
        self.contents.font = [UIFont systemFontOfSize:19 * KScreenWidth / 414];
//        self.contents.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.contents];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 2;
        layout.minimumLineSpacing = 2;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(x, self.title.height + self.contents.height, cellWidth, 0) collectionViewLayout:layout];

        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:collectionIdentifier];
      
        
        [self.contentView addSubview:self.collectionView];
        
        
        
        nameHeight = 40 * KScreenWidth / 414;
        self.name = [[UILabel alloc]init];
        self.name.font = [UIFont systemFontOfSize:14 * KScreenWidth / 414];
        self.name.textColor = [UIColor grayColor];
//        self.name.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.name];
        
        self.time = [[UILabel alloc]init];
        self.time.textColor = [UIColor grayColor];
        self.time.textAlignment = NSTextAlignmentRight;
        self.time.font = [UIFont systemFontOfSize:14 * KScreenWidth / 414];
        [self.contentView addSubview:self.time];
        
        //UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(x, self.title.height + self.contents.height + self.name.height - 5, cellWidth, 0.5)];
        self.line = [[UILabel alloc]init];
        self.line.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.line];
        
    }
    return self;
}

- (void)setRowModel:(StrategyDetailRowModel *)rowModel
{
    self.collectionView.height = 0;
    _rowModel = rowModel;
    self.title.height = 0;
    self.time.height = 0;
    self.title.text = @"";
    self.name.text = @"";
    self.time.text = @"";
    self.contents.text = @"";
    if (![rowModel.title isEqualToString:@""])
    {
        self.title.text = rowModel.title;
        self.title.frame = CGRectMake(x, 0, cellWidth, 40 * KScreenWidth/414);
    }
    
    // 创建实例
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:rowModel.myDescription];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    // 设置行间距
    [style setLineSpacing:6 * KScreenWidth / 414];
    self.contents.numberOfLines = 0;
    // range 适用于哪一段字体
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, rowModel.myDescription.length)];
 
    CGRect bounds = [rowModel.myDescription boundingRectWithSize:CGSizeMake(cellWidth, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.contents.font,NSParagraphStyleAttributeName:style} context:nil];
//    NSLog(@"%@", rowModel.myDescription);
//    NSLog(@"%@", attStr);
    self.contents.attributedText = attStr;
    self.contents.frame = CGRectMake(x, self.title.height, cellWidth, bounds.size.height + 5 * KScreenWidth / 414);
    
    if (rowModel.photo.count > 0)
    {
      
        self.collectionView.frame = CGRectMake(x, self.title.height + self.contents.height, cellWidth, 120 * KScreenWidth / 414 + 5 * KScreenWidth / 414);
        self.photoArray = [NSMutableArray array];
        self.photoArray = rowModel.photo;
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView reloadData];

    }

    if (rowModel.name != nil)
    {
        self.name.text = rowModel.name;
        self.name.frame = CGRectMake(x, self.title.height + self.contents.height + self.collectionView.height, cellWidth / 2, nameHeight);
    }
    if ((NSNull *)rowModel.travel_date != [NSNull null])
    {
        self.time.text = rowModel.travel_date;
        self.time.frame = CGRectMake(x + cellWidth / 2, self.title.height + self.contents.height + self.collectionView.height, cellWidth / 2, nameHeight);
    }
    
    self.line.frame = CGRectMake(x, self.title.height + self.contents.height + self.time.height + self.collectionView.height, cellWidth, 0.2);

}

+ (CGFloat)heightForCell:(StrategyDetailRowModel *)model
{
    CGFloat titleHeight = 0;
    if (![model.title isEqualToString:@""])
    {
        titleHeight = 40 * KScreenWidth / 414;
    }
    CGFloat phototHeight = 0;
    if (model.photo.count > 0)
    {
        phototHeight = 120 * KScreenWidth / 414 + 5 * KScreenWidth / 414;
    }
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.lineSpacing = 6 * KScreenWidth / 414;
    CGFloat contentHeight = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - 40 * KScreenWidth / 414, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19 * KScreenWidth / 414],NSParagraphStyleAttributeName:style} context:nil].size.height + 5 * KScreenWidth / 414;
//    NSLog(@"contentHeight %f", contentHeight);
    
    CGFloat nameHeight = 0;
    if (model.name != nil || (NSNull *)model.travel_date != [NSNull null])
    {
        nameHeight = 40 * KScreenWidth / 414;
    }
    return titleHeight + phototHeight + contentHeight + nameHeight + 0.2 * KScreenWidth / 414;
    
}

#pragma mark -- collectView --
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *photoCell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionIdentifier forIndexPath:indexPath];
    PhotosModel *model = self.photoArray[indexPath.item];
//    NSLog(@"%@", model);
    photoCell.model = model;
    return photoCell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosModel *model = self.photoArray[indexPath.item];
    CGFloat height = 120 * KScreenWidth / 414;
    CGFloat wigth = height * model.image_width / model.image_height;
    return CGSizeMake(wigth, height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger integer = 0;
    PhotosModel *model = self.photoArray[indexPath.item];
    for (NSInteger i = 0; i < self.photoArray.count; i++)
    {
        if ([model isEqual:self.photoArray[i]])
        {
            integer = i;
        }
    }
    if ([self.delegate respondsToSelector:@selector(JumpPage:item:)])
    {
        [self.delegate JumpPage:self.photoArray item:integer];
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
